package org.aaron.wbclient.model;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.aaron.wbclient.R;
import org.aaron.wbclient.utils.ImageLoaderUtils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class WeiBoAdapter extends BaseAdapter {

	private WeiBoHomeList weiBoHomeList;
	private List<WeiBoData> info;
	private Map<Integer, View> rowViews = new HashMap<Integer, View>();
	private Context context;

	public WeiBoAdapter(WeiBoHomeList weiBoHomeList, Context context) {
		super();
		this.weiBoHomeList = weiBoHomeList;
		this.info = weiBoHomeList.getData().getInfo();
		this.context = context;
	}

	@Override
	public int getCount() {
		return info.size();
	}

	@Override
	public Object getItem(int position) {
		return info.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View rowView = rowViews.get(position);
		if (rowView == null) {
			LayoutInflater inflater = LayoutInflater.from(context);
			rowView = inflater.inflate(R.layout.weibolistitem, null);
			LinearLayout layout = (LinearLayout) rowView.findViewById(R.id.linearlayoutId);
			TextView nameView = (TextView) rowView.findViewById(R.id.nameId);
			TextView textView = (TextView) rowView.findViewById(R.id.textId);
			WeiBoData weiBoData = info.get(position);
			nameView.setText(weiBoData.getName());
			textView.setText(weiBoData.getText());
			if (weiBoData.getImage() != null) {
				for (Iterator<String> iterator = weiBoData.getImage().iterator(); iterator.hasNext();) {
					ImageView imageView = new ImageView(context);
					layout.addView(imageView);
					ImageLoaderUtils imageLoaderUtils = new ImageLoaderUtils();
					imageLoaderUtils.loaderDrawable(iterator.next(), new ImageCallBackImpl(imageView));
				}
			}
			rowViews.put(position, rowView);
		}
		return rowView;
	}
}
